package com.framework;

import java.math.BigDecimal;
import java.math.BigInteger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eportal.util.PropertiesUtil;


public class InitListener implements ServletContextListener {

    private static final Log log = LogFactory.getLog(InitListener.class);

    public void contextDestroyed(ServletContextEvent sce) {
        log.info("web exit ... ");
    }

    public void contextInitialized(ServletContextEvent sce) {

        /**BeanUtils注册转换类型**/
        ConvertUtils.deregister();
        ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
        ConvertUtils.register(new SqlTimestampConverter(null), java.sql.Timestamp.class);
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);


//        /**初始化CacheObject类中cityService、tagentService、tdepartmentService**/
//        CityService cityService = (CityService) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("cityService");
//        TagentService tagentService = (TagentService) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("tagentService");
//        TdepartmentService tdepartmentService = (TdepartmentService) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("tdepartmentService");
//        CacheObject.init(cityService, tagentService, tdepartmentService);

        sce.getServletContext().setAttribute("IMG_SERVER",PropertiesUtil.getValue("IMG_SERVER"));
    }
}

